﻿package net.cooltain.warhammer.running.entity;

import java.util.List;

import net.cooltain.utils.CollectionUtils;
import net.cooltain.utils.Logger;
import net.cooltain.warhammer.framework.constant.ConstantPlayZone;
import net.cooltain.warhammer.running.event.MoveCardEvent;

public class DeskZone extends ZoneEntity{


	
	public DeskZone(MotionPlayerZone player) {
		super(player);
		this.zoneConstant = ConstantPlayZone.DESK_ZONE;
	}

	/**
	 * 游戏开始,载入套牌
	 * @param desk
	 */
	public void loadCardToDesk(List<MotionCard> desk){
		this.cards = desk ;
		for(MotionCard card : this.cards){
			card.currentZone = ConstantPlayZone.DESK_ZONE;
		}
		if(player.isLogger){
			System.out.println(new StringBuffer()
				.append(player.playerName).append(" ").append("加载了套牌.")
				.append("(总张数:").append(desk.size()).append(")")
			);
		}
	}
	
	/**
	 * 洗牌
	 */
	public void shuffleDesk(){
		if(CollectionUtils.notEmpty(this.cards)){
			this.cards = CollectionUtils.randomSort(this.cards);
		}
		if(player.isLogger){
			System.out.println(new StringBuffer()
				.append(player.playerName).append(" ").append("洗牌.")
			);
		}
	}
	

	/**
	 * 抽一张牌
	 */
	public void drawTopCard(){
		if(CollectionUtils.isEmpty(this.cards)){
			if(player.isLogger){
				Logger.info(new StringBuffer()
					.append(player.playerName).append(" 抽牌堆没有牌可以抽了.")
				);
			}
			player.playerLoser();
			return ;
		}
		int index = this.cards.size()-1;
		MotionCard card = this.cards.get( index );// 抽序号最大的
		MoveCardEvent.moveACard(card, ConstantPlayZone.HAND_ZONE);
		if(player.isLogger){
			Logger.info(new StringBuffer()
				.append(player.playerName).append(" 抽了一张牌.")
				.append("(手:").append(player.handZone.getCardSize())
				.append("|牌:").append(this.cards.size()).append(")")
			);
		}
	}
	
	/**
	 * 抽n张牌
	 * @param number
	 */
	public void drawTopCard(int number){
		if(number>=0){
			boolean tmpLogger = player.isLogger;
			player.isLogger = false;
			for(int i=0; i<number; i++){				
				drawTopCard();
			}
			player.isLogger = tmpLogger ;
			if(player.isLogger){
				Logger.info(new StringBuffer()
					.append(player.playerName).append(" 抽了").append(number).append("张牌.")
					.append("(手:").append(player.handZone.getCardSize())
					.append("|牌:").append(this.cards.size()).append(")")
				);
			}
		}else{
			if(player.isLogger){
				Logger.warn(new StringBuffer()
					.append("WARNNING ")
					.append(player.playerName).append(" 抽牌数不可以小于0")
				);
			}
		}
	}
	
	/**
	 * 清除所有卡的可见权限
	 */
	public void clearAllVisible(){
		for(MotionCard card : this.cards){
			card.setVisiblePlayers(null);
		}
	}


	@Override
	public void eventAddCard(MotionCard card) {
		super.eventAddCard(card);
		// 默认双方都不可以看到抽牌堆
		card.setVisiblePlayers(null);
	}
	
	
	
}
